using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent (typeof (MeshCollider))]
[RequireComponent (typeof (MeshFilter))]
[RequireComponent (typeof (MeshRenderer))]
public class PaperSheet : MonoBehaviour
{

	private Mesh myMesh;
	private Vector3[] meshVertices;
	private Vector2[] meshUV;
	private int[] meshFaceIndex;
	
	public bool sharedVertices = true;
	
	public void Rebuild(string build)
	{
		Debug.Log(build);
		MeshFilter meshFilter = GetComponent<MeshFilter>();
		if (meshFilter==null){
			Debug.LogError("MeshFilter not found!");
			return;
		}

		//create face
		meshVertices = new Vector3[4];
		meshVertices[0] = new Vector3(-1,-1,-1);
		meshVertices[1] = new Vector3(1,-1,-1);
		meshVertices[2] = new Vector3(1,1,-1);
		meshVertices[3] = new Vector3(-1,1,-1);
		
		meshUV = new Vector2[4];
		meshUV[0] = new Vector2(0,0);
		meshUV[1] = new Vector2(1,0);
		meshUV[2] = new Vector2(1,1);
		meshUV[3] = new Vector2(0,1);
		
		meshFaceIndex = new int[6];
		meshFaceIndex[0] = 0;
		meshFaceIndex[1] = 1;
		meshFaceIndex[2] = 2;
		meshFaceIndex[3] = 2;
		meshFaceIndex[4] = 3;
		meshFaceIndex[5] = 0;
		
		myMesh = meshFilter.sharedMesh;
		myMesh.Clear();
		//GetComponent<MeshFilter>().mesh = myMesh;
		myMesh.vertices = meshVertices;
		myMesh.uv = meshUV;
		myMesh.triangles = meshFaceIndex;
		myMesh.RecalculateNormals();
		myMesh.RecalculateBounds();
		myMesh.Optimize();
	}
	
	// Use this for initialization
	void Start () {
		Rebuild("Start");
		Debug.Log ("Start called");
	}
	
	// Update is called once per frame
	void Update () {
		//Debug.Log ("Update called");
	}

    public void Cut(List<Vector2> toCut)
    {
		if (toCut == null)
		{
			Rebuild("Rebuild");
			return ;
		}
		
        Vector3 first, second, third, fourth;
        first = new Vector3(toCut[0].x, toCut[0].y, -1);
        toCut.RemoveAt(0);
        second = new Vector3(toCut[0].x, toCut[0].y, -1);
        toCut.RemoveAt(0);
        third = new Vector3(toCut[0].x, toCut[0].y, -1);
        toCut.RemoveAt(0);
        fourth = new Vector3(toCut[0].x, toCut[0].y, -1);
        toCut.Clear();

        //modifying the inner geometry of the page
        Vector3[] meshVertices2 = new Vector3[8];
        meshVertices2[0] = meshVertices[0];
        meshVertices2[1] = meshVertices[1];
        meshVertices2[2] = meshVertices[2];
        meshVertices2[3] = meshVertices[3];

        meshVertices2[4] = first;
        meshVertices2[5] = second;
        meshVertices2[6] = third;
        meshVertices2[7] = fourth;

        meshVertices = meshVertices2;

        Vector2[] meshUV2 = new Vector2[8];
        meshUV2[0] = meshUV[0];
        meshUV2[1] = meshUV[1];
        meshUV2[2] = meshUV[2];
        meshUV2[3] = meshUV[3];

        meshUV2[4] = new Vector2((first.x + 1) / 2.0f, (first.y + 1) / 2.0f);
        meshUV2[5] = new Vector2((second.x + 1) / 2.0f, (second.y + 1) / 2.0f);
        meshUV2[6] = new Vector2((third.x + 1) / 2.0f, (third.y + 1) / 2.0f);
        meshUV2[7] = new Vector2((fourth.x + 1) / 2.0f, (fourth.y + 1) / 2.0f);

        meshUV = meshUV2;

        meshFaceIndex = new int[24];
        meshFaceIndex[0] = 0;
        meshFaceIndex[1] = 1;
        meshFaceIndex[2] = 4;
        meshFaceIndex[3] = 4;
        meshFaceIndex[4] = 1;
        meshFaceIndex[5] = 5;

        meshFaceIndex[6] = 5;
        meshFaceIndex[7] = 1;
        meshFaceIndex[8] = 2;
        meshFaceIndex[9] = 2;
        meshFaceIndex[10] = 6;
        meshFaceIndex[11] = 5;

        meshFaceIndex[12] = 2;
        meshFaceIndex[13] = 3;
        meshFaceIndex[14] = 6;
        meshFaceIndex[15] = 6;
        meshFaceIndex[16] = 3;
        meshFaceIndex[17] = 7;

        meshFaceIndex[18] = 7;
        meshFaceIndex[19] = 3;
        meshFaceIndex[20] = 0;
        meshFaceIndex[21] = 0;
        meshFaceIndex[22] = 4;
        meshFaceIndex[23] = 7;

        MeshFilter meshFilter = GetComponent<MeshFilter>();

        myMesh = meshFilter.sharedMesh;
        myMesh.Clear();
        //GetComponent<MeshFilter>().mesh = myMesh;
        myMesh.vertices = meshVertices;
        myMesh.uv = meshUV;
        myMesh.triangles = meshFaceIndex;
        myMesh.RecalculateNormals();
        myMesh.RecalculateBounds();
        myMesh.Optimize();
    }
}
